<?php
class stock_model extends CI_Model{
	private $kodeStok;
	private $kodeSupplier;
	private $kodeDetailBarang;
	private $stockAkhir;
	private $Quantity;
	private $Status;
	private $CreatedBy;
	private $CreatedOn;
	private $LastUpdateBy;
	private $LastUpdateOn;
	private $Active;

	function setKodeStok($kStok){
		$this->kodeStok = $kStok;
	}
	function getKodeStok(){
		return $this->kodeStok;
	}
	
	function setKodeSupplier($kSup){
		$this->kodeSupplier = $kSup;
	}
	function getKodeSupplier(){
		return $this->kodeSupplier;
	}
	
	function setKodeDetailBarang($kDetailBarang){
		$this->kodeDetailBarang = $kDetailBarang;
	}
	function getKodeDetailBarang(){
		return $this->kodeDetailBarang;
	}
	
	function setStokAkhir($sAkhir){
		$this->stockAkhir = $sAkhir;
	}
	function getStokAkhir(){
		return $this->stockAkhir;
	}
	
	function setQuantity($sAwal){
		$this->Quantity = $sAwal;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setStatus($Status){
		$this->Status = $Status;
	}
	function getStatus(){
		return $this->Status;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}
	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}

	// Call the module constructor
	function __construct(){
		parent::__construct();
	}

	function ambilkodestock()
	{
		$query = $this->db->query("select MAX(RIGHT(KodeStok,4)) as kdstck from stock");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idkdstck)
			{
				$tmpkdstck = ((int)$idkdstck->kdstck)+1;
				$kd = sprintf("%04s", $tmpkdstck);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "ST".$kd; 
		
		return $kd;
	}
	function insert(){
		$data = array(
			'KodeStok' => $this->getKodeStok(),
			'KodeSupplier' => $this->getKodeSupplier(),
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			'StockAkhir' => $this->getStokAkhir(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => 'null',
			'Lastupdateon' => 'null',
			'Active' => '1'						
		);
		$this->db->insert('stock',$data);
	}
	
	function insertdetail(){
		$data = array(
			'KodeStok' => $this->getKodeStok(),
			'Quantity' => $this->getQuantity(),
			'Status' => $this->getStatus(),
			'CreatedBy' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Active' => '1'						
		);
		$this->db->insert('transaction_stock',$data);
	}
	
	function cekkodestck()
	{	
		$query = $this->db->query("SELECT count(kodestok) as kdstok FROM stock WHERE KodeSupplier = '".$this->getKodeSupplier()."' AND KodeDetailBarang = '".$this->getKodeDetailBarang()."'");
		$stok = "";
		
		foreach($query->result() as $d)
		{
			$stok = $d->kdstok;
		}
		return $stok;		
	}
	
	function getStockListId()
	{	
		$this->db->select('kodestok')
		->where(array('KodeSupplier' => $this->getKodeSupplier(),'KodeDetailBarang'=>$this->getKodeDetailBarang()));
		$query = $this->db->get('stock');
		
		return $query->result();
		
	}
	
	function getStockCurrent(){
		$this->db->select('stockakhir')
		->where(array('KodeSupplier' => $this->getKodeSupplier(),'KodeDetailBarang'=>$this->getKodeDetailBarang()));
		$query = $this->db->get('stock');
		return $query->result();
	}

	function update(){
		$data = array();
		if($this->getKodeSupplier()){
			$data['KodeSupplier'] = $this->getKodeSupplier();
		}
		if($this->getKodeDetailBarang()){
			$data['KodeDetailBarang'] = $this->getKodeDetailBarang();
		}
		if($this->getStokAkhir()){
			$data['StockAkhir'] = $this->getStokAkhir();
		}
		if($this->getLastUpdateBy()){
			$data['lastupdateby'] = $this->getLastUpdateBy();
		}
		if($this->getLastUpdateOn()){
			$data['lastupdateon'] = $this->getLastUpdateOn();
		}
		$this->db->where('KodeStok',$this->getKodeStok());
		$this->db->update('stock',$data);
	}	
}